
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using System.Collections.Generic;
using System.Globalization;
using WpfApp_SmartParking_Entities;

namespace WpfApp_SmartParking_Orm;

public class EFDbContext : DbContext
{
    public EFDbContext(DbContextOptions<EFDbContext> options)
            : base(options)
    { }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        if (!optionsBuilder.IsConfigured)
            optionsBuilder.UseSqlServer("Data Source=cli;Initial Catalog=DbSmarkParting;User ID=sa;Password=anchor;Encrypt=False");
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        ValueConverter iconValueConvert = new ValueConverter<string?, string?>(
            p2d => string.IsNullOrEmpty(p2d) ? null : ((int)p2d.ToArray()[0]).ToString("x"),
            d2p => d2p == null ? "" : ((char)int.Parse(d2p, NumberStyles.HexNumber)).ToString());
        modelBuilder.Entity<SysMenu>()
            .Property(m => m.MenuIcon)
            .HasConversion(iconValueConvert);
    }

    public virtual DbSet<SysUser> SysUser { get; set; }
    public virtual DbSet<SysMenu> SysMenu { get; set; }
}

